package class4;

/**
 * https://leetcode.cn/problems/bitwise-and-of-numbers-range/
 * 数字范围按位与
 * 解题思路：
 * 1. right 不停的取最后一位1，直到left==right时，right就是 left 和 right 范围内的按位与的值
 */
public class Code10_rangeBitwiseAnd {
    public static void main(String[] args) {
        int left = 5, right = 7;
        int i = rangeBitwiseAnd(left, right);
        System.out.println("i = " + i);
    }

    public static int rangeBitwiseAnd(int left, int right) {
        // 0100
        // 0100
        while (left < right) {
            right -= right & -right;
        }
        return right;
    }
}
